<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>vue组件传值的多种方式 | dmf的个人博客</title><meta name="author" content="dmf"><meta name="copyright" content="dmf"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="描述">
<meta property="og:type" content="article">
<meta property="og:title" content="vue组件传值的多种方式">
<meta property="og:url" content="https://gitee.com/dmf-c/blog/2022/01/02/vue_file/assembly/index.html">
<meta property="og:site_name" content="dmf的个人博客">
<meta property="og:description" content="描述">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img1.baidu.com/it/u=1828439677,2166285235&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=300">
<meta property="article:published_time" content="2022-01-01T16:00:00.000Z">
<meta property="article:modified_time" content="2022-11-04T00:52:30.615Z">
<meta property="article:author" content="dmf">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img1.baidu.com/it/u=1828439677,2166285235&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=300"><link rel="shortcut icon" href="/blog/img/favicon.png"><link rel="canonical" href="https://gitee.com/dmf-c/blog/2022/01/02/vue_file/assembly/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'vue组件传值的多种方式',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-11-04 08:52:30'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">0</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw iconfont icon-shouyex"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-zuixinwenzhang_huaban"></i><span> 找文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw iconfont icon-fenlei1"></i><span> 分类</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw iconfont icon-biaoqian1"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw iconfont icon-shijianzhou"></i><span> 归档</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">dmf的个人博客</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw iconfont icon-shouyex"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-zuixinwenzhang_huaban"></i><span> 找文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw iconfont icon-fenlei1"></i><span> 分类</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw iconfont icon-biaoqian1"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw iconfont icon-shijianzhou"></i><span> 归档</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">vue组件传值的多种方式</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="fa-fw post-meta-icon far fa-calendar-alt"></i><span class="post-meta-label">发表于</span><time datetime="2022-01-01T16:00:00.000Z" title="发表于 2022-01-02 00:00:00">2022-01-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/vue/">vue</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="vue组件传值的多种方式"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="vue组件间的多种传值方式"><a href="#vue组件间的多种传值方式" class="headerlink" title="vue组件间的多种传值方式"></a>vue组件间的多种传值方式</h2><p>当做到比较大的项目时，一个vue文件里面可能要包含上千行代码，这样不利于后期维护与问题定位，抽离成组件就尤其重要了</p>
<p>而重中之重的就是组件之间数据传递的几种方式</p>
<p>Vue2最常见的12种组件间的通讯方式</p>
<pre><code>props
$emit
.sync
v-model
ref
$children / $parent
$attrs / $listeners
provide / inject
EventBus
Vuex
$root
slot
</code></pre>
<h3 id="props"><a href="#props" class="headerlink" title="props"></a>props</h3><p>父组件向子组件传送数据，这应该是最常用的方式了</p>
<p>子组件接收到数据之后，不能直接修改父组件的数据。</p>
<p>否则会报错，因为当父组件重新渲染时，数据会被覆盖。如果只在子组件内要修改的话推荐使用 computed</p>
<p>Parent.vue 传送:<br><template><br>    <child :msg="msg"></child><br></template></p>
<pre><code>Child.vue 接收:
export default &#123;
// 写法一 用数组接收
props:[&#39;msg&#39;],
// 写法二 用对象接收，可以限定接收的数据类型、设置默认值、验证等
props:&#123;
    msg:&#123;
        type:String,
        default:&#39;这是默认数据&#39;
    &#125;
&#125;,
mounted()&#123;
    console.log(this.msg)
&#125;,
&#125;
</code></pre>
<h3 id="sync-子组件可以修改父组件内容"><a href="#sync-子组件可以修改父组件内容" class="headerlink" title=".sync 子组件可以修改父组件内容"></a>.sync 子组件可以修改父组件内容</h3><p>.sync可以帮我们实现父组件向子组件传递的数据的双向绑定，所以子组件接收到数据后可以直接修改，并且会同时修改父组件的数据</p>
<pre><code>Parent.vue:
&lt;template&gt;
    &lt;child :page.sync=&quot;page&quot;&gt;&lt;/child&gt;
&lt;/template&gt;
&lt;script&gt;
export default &#123;
    data()&#123;
        return &#123;
            page:1
        &#125;
    &#125;
&#125;
&lt;/script&gt;

Child.vue:
export default &#123;
    props:[&quot;page&quot;],
    computed()&#123;
        // 当我们在子组件里修改 currentPage 时，父组件的 page 也会随之改变
        currentPage &#123;
            get()&#123;
                return this.page
            &#125;,
            set(newVal)&#123;
                this.$emit(&quot;update:page&quot;, newVal)
            &#125;
        &#125;
    &#125;
&#125;
</code></pre>
<h3 id="v-model"><a href="#v-model" class="headerlink" title="v-model"></a>v-model</h3><p>和 .sync 类似，可以实现将父组件传给子组件的数据为双向绑定，子组件通过 $emit 修改父组件的数据</p>
<pre><code>Parent.vue:
&lt;template&gt;
    &lt;child v-model=&quot;value&quot;&gt;&lt;/child&gt;
&lt;/template&gt;
&lt;script&gt;
export default &#123;
    data()&#123;
        return &#123;
            value:1
        &#125;
    &#125;
&#125;
&lt;/script&gt;

Child.vue:
&lt;template&gt;
    &lt;input :value=&quot;value&quot; @input=&quot;handlerChange&quot;&gt;
&lt;/template&gt;
export default &#123;
    props:[&quot;value&quot;],
    // 可以修改事件名，默认为 input
    model:&#123;
        event:&quot;updateValue&quot;
    &#125;,
    methods:&#123;
        handlerChange(e)&#123;
            this.$emit(&quot;input&quot;, e.target.value)
            // 如果有上面的重命名就是这样
            this.$emit(&quot;updateValue&quot;, e.target.value)
        &#125;
    &#125;
&#125;
</code></pre>
<h3 id="ref"><a href="#ref" class="headerlink" title="ref"></a>ref</h3><p>ref 如果在普通的DOM元素上，引用指向的就是该DOM元素;</p>
<p>如果在子组件上，引用的指向就是子组件实例;</p>
<p>父组件可以通过 ref 主动获取子组件的属性或者调用子组件的方法</p>
<pre><code>// Child.vue
export default &#123;
    data()&#123;
        return &#123;
            name:&quot;oldCode&quot;
        &#125;
    &#125;,
    methods:&#123;
        someMethod(msg)&#123;
            console.log(msg)
        &#125;
    &#125;
&#125;
&lt;/script&gt;

// Parent.vue
&lt;template&gt;
    &lt;child ref=&quot;child&quot;&gt;&lt;/child&gt;
&lt;/template&gt;
&lt;script&gt;
export default &#123;
    mounted()&#123;
        const child = this.$refs.child
        console.log(child.name) 
        child.someMethod(&quot;调用了子组件的方法&quot;)
    &#125;
&#125;
</code></pre>
<h3 id="emit-x2F-v-on"><a href="#emit-x2F-v-on" class="headerlink" title="$emit &#x2F; v-on"></a>$emit &#x2F; v-on</h3><p>子组件通过派发事件的方式给父组件数据，或者触发父组件更新等操作</p>
<pre><code>// Child.vue 派发
export default &#123;
data()&#123;
    return &#123; msg: &quot;这是发给父组件的信息&quot; &#125;
&#125;,
methods: &#123;
    handleClick()&#123;
        this.$emit(&quot;sendMsg&quot;,this.msg)
    &#125;
&#125;,
&#125;

// Parent.vue 响应
&lt;template&gt;
    &lt;child v-on:sendMsg=&quot;getChildMsg&quot;&gt;&lt;/child&gt;
    // 或 简写
    &lt;child @sendMsg=&quot;getChildMsg&quot;&gt;&lt;/child&gt;
&lt;/template&gt;

export default &#123;
    methods:&#123;
        getChildMsg(msg)&#123;
            console.log(msg) // 这是父组件接收到的消息
        &#125;
    &#125;
&#125;
</code></pre>
<h3 id="attrs-x2F-listeners"><a href="#attrs-x2F-listeners" class="headerlink" title="$attrs &#x2F; $listeners"></a>$attrs &#x2F; $listeners</h3><p>多层嵌套组件传递数据时，如果只是传递数据，而不做中间处理的话就可以用这个，比如父组件向孙子组件传递数据时</p>
<p>$attrs：包含父作用域里除 class 和 style 除外的非 props 属性集合。通过 this.$attrs 获取父作用域中所有符合条件的属性集合，然后还要继续传给子组件内部的其他组件，就可以通过 v-bind&#x3D;”$attrs”</p>
<p>$listeners：包含父作用域里 .native 除外的监听事件集合。如果还要继续传给子组件内部的其他组件，就可以通过 v-on&#x3D;”$linteners”使用方式是相同的</p>
<pre><code>Parent.vue:
&lt;template&gt;
    &lt;child :name=&quot;name&quot; title=&quot;1111&quot; &gt;&lt;/child&gt;
&lt;/template
export default&#123;
    data()&#123;
        return &#123;
            name:&quot;oldCode&quot;
        &#125;
    &#125;
&#125;

Child.vue:
&lt;template&gt;
    // 继续传给孙子组件
    &lt;sun-child v-bind=&quot;$attrs&quot;&gt;&lt;/sun-child&gt;
&lt;/template&gt;
export default&#123;
    props:[&quot;name&quot;], // 这里可以接收，也可以不接收
    mounted()&#123;
        // 如果props接收了name 就是 &#123; title:1111 &#125;，否则就是&#123; name:&quot;oldCode&quot;, title:1111 &#125;
        console.log(this.$attrs)
    &#125;
&#125;
</code></pre>
<h3 id="children-x2F-parent"><a href="#children-x2F-parent" class="headerlink" title="$children &#x2F; $parent"></a>$children &#x2F; $parent</h3><p>$children：获取到一个包含所有子组件(不包含孙子组件)的 VueComponent 对象数组，可以直接拿到子组件中所有数据和方法等</p>
<p>$parent：获取到一个父节点的 VueComponent 对象，同样包含父节点中所有数据和方法等</p>
<pre><code>Parent.vue:
export default&#123;
    mounted()&#123;
        this.$children[0].someMethod() // 调用第一个子组件的方法
        this.$children[0].name // 获取第一个子组件中的属性
    &#125;
&#125;

Child.vue:
export default&#123;
    mounted()&#123;
        this.$parent.someMethod() // 调用父组件的方法
        this.$parent.name // 获取父组件中的属性
    &#125;
&#125;
</code></pre>
<h3 id="provide-x2F-inject"><a href="#provide-x2F-inject" class="headerlink" title="provide &#x2F; inject"></a>provide &#x2F; inject</h3><p>provide &#x2F; inject 是依赖注入，在一些插件或组件库里被常用</p>
<p>provide：可以让我们指定想要提供给后代组件的数据或方法</p>
<p>inject：在任何后代组件中接收想要添加在这个组件上的数据或方法，不管组件嵌套多深都可以直接拿来用</p>
<p>要注意的是 provide 和 inject 传递的数据不是响应式的，也就是说用 inject 接收来数据后，provide 里的数据改变了，后代组件中的数据不会改变，除非传入的就是一个可监听的对象 所以建议还是传递一些常量或者方法</p>
<pre><code>// 父组件
export default&#123;
    // 方法一 不能获取 methods 中的方法
    provide:&#123;
        name:&quot;oldCode&quot;,
        age: this.data中的属性
    &#125;,
    // 方法二 不能获取 data 中的属性
    provide()&#123;
        return &#123;
            name:&quot;oldCode&quot;,
            someMethod:this.someMethod // methods 中的方法
        &#125;
    &#125;,
    methods:&#123;
        someMethod()&#123;
            console.log(&quot;这是注入的方法&quot;)
        &#125;
    &#125;
&#125;

// 后代组件
export default&#123;
    inject:[&quot;name&quot;,&quot;someMethod&quot;],
    mounted()&#123;
        console.log(this.name)
        this.someMethod()
    &#125;
&#125;
</code></pre>
<h3 id="EventBus"><a href="#EventBus" class="headerlink" title="EventBus"></a>EventBus</h3><p>EventBus 是中央事件总线，不管是父子组件，兄弟组件，跨层级组件等都可以使用它完成通信操作</p>
<pre><code>// 方法一
// 抽离成一个单独的 js 文件 Bus.js ，然后在需要的地方引入
// Bus.js
import Vue from &quot;vue&quot;
export default new Vue()

// 方法二 直接挂载到全局
// main.js
import Vue from &quot;vue&quot;
Vue.prototype.$bus = new Vue()

// 方法三 注入到 Vue 根对象上
// main.js
import Vue from &quot;vue&quot;
new Vue(&#123;
    el:&quot;#app&quot;,
    data:&#123;
        Bus: new Vue()
    &#125;
&#125;)
</code></pre>
<p>使用如下，以方法一按需引入为例</p>
<pre><code>// 在需要向外部发送自定义事件的组件内
&lt;template&gt;
    &lt;button @click=&quot;handlerClick&quot;&gt;按钮&lt;/button&gt;
&lt;/template&gt;
import Bus from &quot;./Bus.js&quot;
export default&#123;
    methods:&#123;
        handlerClick()&#123;
            // 自定义事件名 sendMsg
            Bus.$emit(&quot;sendMsg&quot;, &quot;这是要向外部发送的数据&quot;)
        &#125;
    &#125;
&#125;

// 在需要接收外部事件的组件内
import Bus from &quot;./Bus.js&quot;
export default&#123;
    mounted()&#123;
        // 监听事件的触发
        Bus.$on(&quot;sendMsg&quot;, data =&gt; &#123;
            console.log(&quot;这是接收到的数据：&quot;, data)
        &#125;)
    &#125;,
    beforeDestroy()&#123;
        // 取消监听
        Bus.$off(&quot;sendMsg&quot;)
    &#125;
&#125;
</code></pre>
<h3 id="Vuex"><a href="#Vuex" class="headerlink" title="Vuex"></a>Vuex</h3><p>Vuex 是状态管理器，集中式存储管理所有组件的状态。</p>
<p>新建一个index.js文件， 里内容如下</p>
<pre><code>import Vue from &#39;vue&#39;
import Vuex from &#39;vuex&#39;
import getters from &#39;./getters&#39;
import actions from &#39;./actions&#39;
import mutations from &#39;./mutations&#39;
import state from &#39;./state&#39;
import user from &#39;./modules/user&#39;

Vue.use(Vuex)

const store = new Vuex.Store(&#123;
modules: &#123;
    user
&#125;,
getters,
actions,
mutations,
state
&#125;)
export default store
</code></pre>
<p>然后在 main.js 引入</p>
<pre><code>import Vue from &quot;vue&quot;
import store from &quot;./store&quot;
new Vue(&#123;
    el:&quot;#app&quot;,
    store,
    render: h =&gt; h(App)
&#125;)
</code></pre>
<p>然后在需要的使用组件里</p>
<pre><code>import &#123; mapGetters, mapMutations &#125; from &quot;vuex&quot;
export default&#123;
    computed:&#123;
        // 方式一 然后通过 this.属性名就可以用了
        ...mapGetters([&quot;引入getters.js里属性1&quot;,&quot;属性2&quot;])
        // 方式二
        ...mapGetters(&quot;user&quot;, [&quot;user模块里的属性1&quot;,&quot;属性2&quot;])
    &#125;,
    methods:&#123;
        // 方式一 然后通过 this.属性名就可以用了
        ...mapMutations([&quot;引入mutations.js里的方法1&quot;,&quot;方法2&quot;])
        // 方式二
        ...mapMutations(&quot;user&quot;,[&quot;引入user模块里的方法1&quot;,&quot;方法2&quot;])
    &#125;
&#125;

// 或者也可以这样获取
this.$store.state.xxx
this.$store.state.user.xxx
</code></pre>
<h3 id="root"><a href="#root" class="headerlink" title="$root"></a>$root</h3><p>1、 作用：访问根组件中的属性或方法</p>
<p>2、 注意：是根组件，不是父组件。$root只对根组件有用</p>
<h3 id="slot插槽"><a href="#slot插槽" class="headerlink" title="slot插槽"></a>slot插槽</h3><p>就是把子组件的数据通过插槽的方式传给父组件使用，然后再插回来</p>
<pre><code>Child.vue:
&lt;template&gt;
    &lt;div&gt;
        &lt;slot :user=&quot;user&quot;&gt;&lt;/slot&gt;
    &lt;/div&gt;
&lt;/template&gt;
export default&#123;
    data()&#123;
        return &#123;
            user:&#123; name:&quot;oldCode&quot; &#125;
        &#125;
    &#125;
&#125;

Parent.vue:
&lt;template&gt;
    &lt;div&gt;
        &lt;child v-slot=&quot;slotProps&quot;&gt;
            &#123;&#123; slotProps.user.name &#125;&#125;
        &lt;/child&gt;
    &lt;/div&gt;
&lt;/template&gt;
</code></pre>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://gitee.com/dmf-c/blog">dmf</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://gitee.com/dmf-c/blog/2022/01/02/vue_file/assembly/">https://gitee.com/dmf-c/blog/2022/01/02/vue_file/assembly/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://gitee.com/dmf-c/blog" target="_blank">dmf的个人博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2022/01/16/vue_file/vuex/"><img class="prev-cover" src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Vuex使用方法</div></div></a></div><div class="next-post pull-right"><a href="/blog/2021/12/18/mysql/"><img class="next-cover" src="https://img1.baidu.com/it/u=3691791879,443411760&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=500&amp;h=889" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">mysql常用函数以及方法</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">dmf</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">0</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">业精于勤，荒于嬉。</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#vue%E7%BB%84%E4%BB%B6%E9%97%B4%E7%9A%84%E5%A4%9A%E7%A7%8D%E4%BC%A0%E5%80%BC%E6%96%B9%E5%BC%8F"><span class="toc-number">1.</span> <span class="toc-text">vue组件间的多种传值方式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#props"><span class="toc-number">1.1.</span> <span class="toc-text">props</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#sync-%E5%AD%90%E7%BB%84%E4%BB%B6%E5%8F%AF%E4%BB%A5%E4%BF%AE%E6%94%B9%E7%88%B6%E7%BB%84%E4%BB%B6%E5%86%85%E5%AE%B9"><span class="toc-number">1.2.</span> <span class="toc-text">.sync 子组件可以修改父组件内容</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#v-model"><span class="toc-number">1.3.</span> <span class="toc-text">v-model</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ref"><span class="toc-number">1.4.</span> <span class="toc-text">ref</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#emit-x2F-v-on"><span class="toc-number">1.5.</span> <span class="toc-text">$emit &#x2F; v-on</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#attrs-x2F-listeners"><span class="toc-number">1.6.</span> <span class="toc-text">$attrs &#x2F; $listeners</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#children-x2F-parent"><span class="toc-number">1.7.</span> <span class="toc-text">$children &#x2F; $parent</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#provide-x2F-inject"><span class="toc-number">1.8.</span> <span class="toc-text">provide &#x2F; inject</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#EventBus"><span class="toc-number">1.9.</span> <span class="toc-text">EventBus</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Vuex"><span class="toc-number">1.10.</span> <span class="toc-text">Vuex</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#root"><span class="toc-number">1.11.</span> <span class="toc-text">$root</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#slot%E6%8F%92%E6%A7%BD"><span class="toc-number">1.12.</span> <span class="toc-text">slot插槽</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2023/01/08/vue_file/vuenote/" title="vue3入门笔记"><img src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="vue3入门笔记"/></a><div class="content"><a class="title" href="/blog/2023/01/08/vue_file/vuenote/" title="vue3入门笔记">vue3入门笔记</a><time datetime="2023-01-07T16:00:00.000Z" title="发表于 2023-01-08 00:00:00">2023-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/02/20/vue_file/comm/" title="vue组件间通信六种方式"><img src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="vue组件间通信六种方式"/></a><div class="content"><a class="title" href="/blog/2022/02/20/vue_file/comm/" title="vue组件间通信六种方式">vue组件间通信六种方式</a><time datetime="2022-02-19T16:00:00.000Z" title="发表于 2022-02-20 00:00:00">2022-02-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/02/05/wxEcharts/" title="微信小程序使用 echarts"><img src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="微信小程序使用 echarts"/></a><div class="content"><a class="title" href="/blog/2022/02/05/wxEcharts/" title="微信小程序使用 echarts">微信小程序使用 echarts</a><time datetime="2022-02-04T16:00:00.000Z" title="发表于 2022-02-05 00:00:00">2022-02-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/01/30/vue_file/vue_router/" title="vue-router学习"><img src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="vue-router学习"/></a><div class="content"><a class="title" href="/blog/2022/01/30/vue_file/vue_router/" title="vue-router学习">vue-router学习</a><time datetime="2022-01-29T16:00:00.000Z" title="发表于 2022-01-30 00:00:00">2022-01-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/01/16/vue_file/vuex/" title="Vuex使用方法"><img src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Vuex使用方法"/></a><div class="content"><a class="title" href="/blog/2022/01/16/vue_file/vuex/" title="Vuex使用方法">Vuex使用方法</a><time datetime="2022-01-15T16:00:00.000Z" title="发表于 2022-01-16 00:00:00">2022-01-16</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By dmf</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>